import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useTokenStore = defineStore('token', () => {
  const token = ref(localStorage.getItem('token') || '')
  
  const hasToken = computed(() => !!token.value)
  
  function setToken(newToken) {
    token.value = newToken
    localStorage.setItem('token', newToken)
  }
  
  function clearToken() {
    token.value = ''
    localStorage.removeItem('token')
    localStorage.removeItem('user')
  }
  
  return {
    token,
    hasToken,
    setToken,
    clearToken
  }
})